﻿using System.Net;
using System.Net.NetworkInformation;

namespace BloggerAutoPosterService.Engine.Net
{
    public static class Downloader
    {
        public static bool IsConnectedToInternet
        {
            get
            {
                bool isConnected = false;

                try
                {
                    if (!NetworkInterface.GetIsNetworkAvailable())
                    {
                        return isConnected;
                    }

                    var req = (HttpWebRequest)WebRequest.Create("http://www.google.com");
                    req.AllowAutoRedirect = true;
                    req.Method = "HEAD";
                    using (var resp = (HttpWebResponse)req.GetResponse())
                    {
                        isConnected = resp.StatusCode.ToString().Equals("OK");
                    }
                }
                catch
                {
                    isConnected = false;
                }

                return isConnected;
            }
        }
    }
}